#include "Container.h"

inline int ResizeIfNecessary(Container *C, int RequestedSize)
{
	if(C->Size >= RequestedSize) /* Nothing has to be done */
		return 0;
	void *Tmp = realloc(C->Array, RequestedSize*C->ElementSize);
	if(Tmp == NULL)
		return -1;
	C->Array = Tmp;
	C->Size = RequestedSize;
	return 0;
}

Container *C__NewContainer(int SizeOfElement)
{
	Container *Ret;
	if((Ret = malloc(sizeof(Ret))) == NULL)
		return NULL;
	Ret->ElementSize = SizeOfElement;
	Ret->Size = 0;
	Ret->Array = NULL;
	return Ret;
}

void C_FreeContainer(Container *C)
{
	/* Go through the whole of the container, freeing all of the elements we
	 * have created
	 */
}

void *C_Get(Container *C, int N)
{
	return C->Array+(N*C->ElementSize);
}

int C_SetElementAt(Container *C, int N, void *Element)
{
	if(ResizeIfNecessary(C, N+1) == -1)
		return -1;
	memcpy(C->Array+(N*C->ElementSize), Element, C->ElementSize);
	return 0;
}
